/*
 *  Copyright (c)  2016-NOW  Jacars Co.Ltd.
 *  All rights reserved.
 */
package com.jacars.biz.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jacars.biz.user.dao.model.JsptRoleMenu;
import com.jacars.biz.user.dao.model.JsptTopMenu;
import com.jacars.biz.user.dao.service.JsptMenuService;
import com.jacars.biz.user.dao.service.JsptTopMenuService;
import com.jacars.biz.user.dto.JsptTopMenuDTO;
import com.jacars.common.api.PageResult;
import com.jacars.common.model.JsptMenu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import com.jacars.common.api.Result;
import com.jacars.db.support.Condition;
import com.jacars.db.support.Query;
import com.jacars.common.utils.FuncUtil;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jacars.biz.user.dao.model.JsptTop;
import com.jacars.biz.user.vo.JsptTopVO;
import com.jacars.biz.user.dao.service.JsptTopService;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 角色菜单关系表 控制器
 *
 * @author JSPT Auto Generate
 * @since 2021-12-26
 */
@RestController
@AllArgsConstructor
@RequestMapping("/topmenu")
@Api(value = "角色菜单关系表", tags = "角色菜单关系表接口")
public class TopController {

	private JsptTopService topMenuService;

	private JsptTopMenuService jsptTopMenuService;

	private JsptMenuService jsptMenuService;
	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入topMenu")
	public Result<JsptTop> detail(JsptTop jsptTop) {
		JsptTop detail = topMenuService.getOne(Condition.getQueryWrapper(jsptTop));
		return Result.succeed(detail);
	}

	/**
	 * 分页 角色菜单关系表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入topMenu")
	public PageResult<JsptTop> list(JsptTop jsptTop, Query query) {
		IPage<JsptTop> pages = topMenuService.page(Condition.getPage(query), Condition.getQueryWrapper(jsptTop));
		List<JsptTop> records = pages.getRecords();
		return PageResult.succeed(records);
	}

	/**
	 * 自定义分页 角色菜单关系表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入topMenu")
	public Result<IPage<JsptTop>> page(JsptTopVO topMenu, Query query) {
		IPage<JsptTop> pages = topMenuService.page(Condition.getPage(query), Condition.getQueryWrapper(topMenu));
		return Result.succeed(pages);
	}

	/**
	 * 新增 角色菜单关系表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入topMenu")
	public Result save(@Valid @RequestBody JsptTop jsptTop) {
		return Result.succeed(topMenuService.save(jsptTop));
	}

	/**
	 * 修改 角色菜单关系表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入topMenu")
	public Result update(@Valid @RequestBody JsptTop jsptTop) {
		return Result.succeed(topMenuService.updateById(jsptTop));
	}

	/**
	 * 新增或修改 角色菜单关系表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入topMenu")
	public Result submit(@Valid @RequestBody JsptTop jsptTop) {
		return Result.succeed(topMenuService.saveOrUpdate(jsptTop));
	}


	/**
	 * 删除 角色菜单关系表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public Result remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return Result.status(topMenuService.removeByIds(FuncUtil.toLongList(ids)));
	}

	/**
	 * 给角色分配菜单
	 */
	@ApiOperation(value = "角色分配菜单")
	@PostMapping("/granted")
	public Result setMenuToRole(@RequestBody JsptTopMenuDTO jsptMenu) {

		Long id = jsptMenu.getId();
		Set<Long> menuIds = jsptMenu.getMenuIds();

		jsptTopMenuService.remove(Wrappers.<JsptTopMenu>lambdaQuery().eq(JsptTopMenu::getTopId, id));

		if (!CollectionUtils.isEmpty(menuIds)) {
			List<JsptTopMenu> topMenus = new ArrayList<>(menuIds.size());
			menuIds.forEach(menuId -> topMenus.add(new JsptTopMenu(id, menuId)));
			jsptTopMenuService.saveBatch(topMenus);
		}
		return Result.succeed("操作成功");
	}

	/**
	 * 给角色分配菜单
	 */
	@ApiOperation(value = "角色分配菜单")
	@GetMapping("/getMenuByTopId")
	public Result<List<String>> getMenuByTopId(JsptTopMenuDTO jsptMenu) {

		Long id = jsptMenu.getId();

		List<JsptTopMenu> list = jsptTopMenuService.list(Wrappers.<JsptTopMenu>lambdaQuery().eq(JsptTopMenu::getTopId, id));

		List<String> menuIds = list.stream().map(k -> FuncUtil.toStr(k.getMenuId())).collect(Collectors.toList());

		return Result.succeed(menuIds, "操作成功");
	}

	/**
	 * 点击顶部菜单，跳转到
	 * @param jsptMenu
	 * @return
	 */
	@ApiOperation(value = "角色分配菜单")
	@GetMapping("/getTopMenuById")
	public Result<List<JsptMenu>> GetTopMenuById(JsptTopMenuDTO jsptMenu) {

		Long id = jsptMenu.getId();

		List<JsptTopMenu> list = jsptTopMenuService.list(Wrappers.<JsptTopMenu>lambdaQuery().eq(JsptTopMenu::getTopId, id));

		List<Long> menuIds = list.stream().map(k -> k.getMenuId()).collect(Collectors.toList());

		QueryWrapper<JsptMenu> jsptMenuQueryWrapper = new QueryWrapper<>();
		jsptMenuQueryWrapper.lambda().in(JsptMenu::getId, menuIds).orderByAsc(JsptMenu::getSort);
		List<JsptMenu> list1 = jsptMenuService.list(jsptMenuQueryWrapper);

		return Result.succeed(treeBuilder(list1));
	}

	/**
	 * 两层循环实现建树
	 *
	 * @param jsptMenus
	 * @return
	 */
	public static List<JsptMenu> treeBuilder(List<JsptMenu> jsptMenus) {
		List<JsptMenu> menus = new ArrayList<>();
		for (JsptMenu jsptMenu : jsptMenus) {
			if (Objects.equals(-1L, jsptMenu.getParentId())) {
				menus.add(jsptMenu);
			}
			for (JsptMenu menu : jsptMenus) {
				if (menu.getParentId().equals(jsptMenu.getId())) {
					if (jsptMenu.getChildren() == null) {
						jsptMenu.setChildren(new ArrayList<>());
					}
					jsptMenu.getChildren().add(menu);
				}
			}
		}
		return menus;
	}
}
